<template>
    <div class="home-template" v-if="loggedInUser">
        <PageTitle>
            <i class="appIcon" v-html="homeIcon"></i>
            <h1>Hello, {{loggedInUser.firstName}}!</h1>
        </PageTitle>

        <div class="row">
            <div  class="col-medium-1-2">
                <Panel>
                    <table v-if="recentItems">
                        <tr>
                            <th width="25%">Recent Item ID</th>
                            <th width="50%">Name</th>
                            <th width="25%">Last Access</th>
                        </tr>
                        <tr v-for="item in recentItems">
                            <td><a href="#">{{item.id}}</a></td>
                            <td>{{item.name}}</td>
                            <td>{{item.lastAccess}}</td>
                        </tr>

                    </table>
                    <div class="no-data" v-else>
                        <img class="no-data__checkmark" src="../assets/img/check.png">
                        <p class="no-data__title">No Recent Items!</p>
                    </div>
                </Panel>
            </div>
            <div  class="col-medium-1-2">
                <Panel>
                    <table v-if="latestObstacles">
                        <tr>
                            <th width="30%">Obstacle Name</th>
                            <th width="70%">Details</th>
                        </tr>
                        <tr v-for="obstacle in latestObstacles">
                            <td>{{obstacle.name}}</td>
                            <td>{{obstacle.details}}</td>
                        </tr>
                    </table>
                    <div class="no-data" v-else>
                        <img class="no-data__checkmark" src="../assets/img/check.png">
                        <p class="no-data__title">No Obstacles!</p>
                    </div>
                </Panel>
            </div>
        </div>
    </div>
</template>

<script>
    import axios from 'axios'
    import Panel from './components/Panel.vue'
    import PageTitle from './components/PageTitle.vue'

    export default {
        components: {
            Panel,
            PageTitle
        },
        data() {
            return {
                homeIcon: require('../assets/img/Home-Icon.svg'),
                loggedInUser : null,
                recentItems : null,
                latestObstacles : null
            }
        },

        mounted: function() {
            var self = this;

            axios.get(process.env.API_ROOT + '/loggedInUser', {
            }).then(function(response) {
                    self.loggedInUser = response.data[0];
                });
            axios.get(process.env.API_ROOT + '/recentItems', {
            }).then(function(response) {
                self.recentItems = response.data;
            });

            axios.get(process.env.API_ROOT + '/recentObstacles', {
            }).then(function(response) {
                self.latestObstacles = response.data;
            });

        },
        methods:  {
            requestTabsClicked: function(e){

                var tab_id = $(e.target).attr('data-tab');

                $('#request-overview-tabs > ul.tabs li').removeClass('current');
                $('#request-overview-tabs > .tab-content').removeClass('current');

                $(e.target).addClass('current');
                $("#"+tab_id).addClass('current');
            },
            assetsTabsClicked: function(e){
                var tab_id = $(e.target).attr('data-tab');

                $('#asset-overview-tabs > ul.tabs li').removeClass('current');
                $('#asset-overview-tabs > .tab-content').removeClass('current');

                $(e.target).addClass('current');
                $("#"+tab_id).addClass('current');
            }
        }
    }
</script>